package cn.tjd.smart_health.admin.controller;


import cn.tjd.smart_health.api.CheckItemService;
import cn.tjd.smart_health.common.constant.MessageConstant;
import cn.tjd.smart_health.common.entity.CheckItem;
import cn.tjd.smart_health.common.pojo.PageSelect;
import cn.tjd.smart_health.common.pojo.QueryBean;
import cn.tjd.smart_health.common.util.R;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @DubboReference
    private CheckItemService checkItemService;

    @PostMapping("/add")
    public R add(@RequestBody CheckItem checkItem){
        try {
            checkItemService.saveCheckItem(checkItem);
            return new R(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }


    @GetMapping("/findAll")
    public R findAll(){
        try{
            List<CheckItem> checkItems = checkItemService.findAllCheckItems();
            return new R(true, MessageConstant.GET_CHECKITEM_LIST_SUCCESS, checkItems);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.GET_CHECKITEM_LIST_FAIL);
        }
    }

    @PostMapping("/findPage")
    public PageSelect<CheckItem> findPage(@RequestBody QueryBean queryBean){
       return checkItemService.findPage(queryBean.getCurrentPage(),queryBean.getPageSize(), queryBean.getQueryString());
    }

    @DeleteMapping("/delete")
    public R delete(@RequestParam("id") Integer id){
        try {
            checkItemService.deleteCheckItem(id);
            return new R(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    @GetMapping("findById.do")
    public R findById(@RequestParam("id") Integer id){
        try {
            CheckItem checkItem = checkItemService.findCheckItemById(id);
            return new R(true, MessageConstant.GET_CHECKITEM_DETAIL_SUCCESS, checkItem);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.GET_CHECKITEM_DETAIL_FAIL);
        }
    }

    @PostMapping("edit.do")
    public R edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.editCheckItem(checkItem);
            return new R(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }


}
